﻿#coding=utf-8
import sys,wx

class CListCtrlAider:
    #按照数据表显示
    @staticmethod
    def initListCtrlColsByDB(listCtrlObj, CDataClass, cols):        
        for i in range(len(CDataClass.m_fieldNames)):
            listCtrlObj.InsertColumn(i, CDataClass.m_fieldCNNames[i])
            if not(CDataClass.m_fieldNames[i] in cols):
                listCtrlObj.SetColumnWidth(i, 0)
                
    @staticmethod
    def refreshListCtrlDataByDB(listview, cls):
        listview.DeleteAllItems() 
        for obj in cls.m_dataList:
            row = listview.InsertStringItem(sys.maxint, '')
            for i in range(len(cls.m_fieldNames)):
                val = obj.m_fieldVals[i]            
                if (val == None):
                    val = ''
                if (type(val) != str and type(val) != unicode):
                    val = str(val)
                
                listview.SetStringItem(row, i, val)
                  
    #按照当前需要的列显示            
    @staticmethod
    def initListCtrlCols(listCtrlObj, cols, CDataClass=None):
        if(CDataClass is None):
            for i in range(len(cols)):
                listCtrlObj.InsertColumn(i, cols[i])
        else:        
            for i in range(len(cols)):            
                n = CDataClass.getFieldNo(cols[i])
                cnName = CDataClass.m_fieldCNNames[n]
                listCtrlObj.InsertColumn(i, cnName)
            
    @staticmethod        
    def refreshListCtrlData(listCtrlObj, cols, CDataClass):
        listCtrlObj.DeleteAllItems() 
        for obj in CDataClass.m_dataList:
            row = listCtrlObj.InsertStringItem(sys.maxint, '')
            for i in range(len(cols)):
                n = CDataClass.getFieldNo(cols[i])
                val = obj.m_fieldVals[n]            
                if (val == None):
                    val = ''
                if (type(val) != str and type(val) != unicode):
                    val = str(val)
                
                listCtrlObj.SetStringItem(row, i, val) 
    
    #直接指示是否显示
    @staticmethod
    def initListCtrlColsAndWidth(listCtrlObj, cols, colWidths, CDataClass=None):         
        if(CDataClass is None):
            for i in range(len(cols)):
                listCtrlObj.InsertColumn(i, cols[i])
                if colWidths[i] != -1:
                    listCtrlObj.SetColumnWidth(i, colWidths[i])
        else:        
            for i in range(len(cols)):
                listCtrlObj.InsertColumn(i, CDataClass.getCNName(cols[i]))
                if colWidths[i] != -1:
                    listCtrlObj.SetColumnWidth(i, colWidths[i])
                       
    @staticmethod
    def appendItem(listCtrlObj, dataList):
        #print 'Add %s' %(dataList)
        row = listCtrlObj.InsertStringItem(sys.maxint, '')
        for i in range(len(dataList)):
            listCtrlObj.SetStringItem(row, i, dataList[i])
            
 
class MyListCtrlDecorator:
    def __init__(self, lc, cols, dataModel, doRefresh=True):
        '''
        cols - [(name, format, width),]
        '''
        self.lc = lc
        self.cols = cols
        self.dataModel = dataModel

        self.initColHeader()
        
        self.filter = {}
        
        if doRefresh:
            self.refresh()
        
    def initColHeader(self):
        for i in range(len(self.cols)):            
            cnName = self.dataModel.getCNName(index=self.cols[i][0])
            self.lc.InsertColumn(i, cnName, self.cols[i][1], self.cols[i][2])
    
    def setFilter(self, filter):
        '''如果设置过滤条件，则仅显示符合条件的项目'''
        self.filter = filter
        
    def checkShow(self, obj):
        for index,val in self.filter.items():
            if obj.getValue(index) != val:
                return False
        return True
                
                
    def refresh(self):
        self.lc.DeleteAllItems()
        dataList = self.dataModel.getObjList()
        for obj in dataList:
            
            if not(self.checkShow(obj)):
                continue
            
            self.append(obj)
    
    def getSeletion(self):
        return self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)

    def getItem(self, row, col=0):
        return self.lc.GetItem(row, col).GetText()
    
    def append(self, obj):
        row = self.lc.InsertStringItem(sys.maxint, '')
        for i in range(len(self.cols)):
            val = obj.getValue(self.cols[i][0])            
            if (val == None):
                val = ''
            if (type(val) != str and type(val) != unicode):
                val = str(val)
            
            self.lc.SetStringItem(row, i, val) 
        
class MyComboBoxDecorator:
    def __init__(self, cb, dataModel, showCol, filter = None):
        '''
            showCol - COL_xx  must be unique
        '''
    
        self.cb = cb
        self.dataModel = dataModel
        self.showCol = showCol
        self.showTest = filter
        
    def refresh(self):
        self.cb.Clear()
        dataList = self.dataModel.getObjList()
        for obj in dataList:
            if (self.showTest is None or self.showTest(obj)):
                self.cb.Append(obj.getValue(self.showCol))
            
    def GetValue(self):
        return self.cb.GetValue()
    
    def SetValue(self, text):
        self.cb.SetValue(text)
    
    def getSelectObj(self):
        dataList = self.dataModel.getObjList()
        for obj in dataList:
            if (self.cb.GetValue() == obj.getValue(self.showCol)):
                return obj
        print ('Unchecked combobox', self.dataModel.__name__)
        return None
        
        
class MyTextCtrlDecorator:
    '''封装客户、供应商、同行的搜索显示，
        当输入改变时，显示的popupWin中的列表也随之改变
        当选中客户时，tc中的相应设置
        
        在DlgFundCustomer中使用时，出现了Show(False)不能使用的情况，所以暂时不用了
        会不会是父句柄变化了？
    '''
    def __init__(self, tc, pWin):
        self.tc = tc
        self.pWin = pWin
        
        self.oldValue = ''
        
        self.tc.Bind(wx.EVT_TEXT, self.OnTextChanged)
        self.tc.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
        
    def OnKillFocus(self, event): 
        print 'OnKillFocus'
        if not self.pWin.IsActive() and event.GetWindow() is not None:
            self.pWin.Dismiss()
        print self.pWin.GetParent()
        #如果为空，则默认选择所有客户
        #如果不是空，有没有选择客户，则认为是原值
        #if (self.GetValue() != ''):
         #   self.ChangeValue(self.oldValue)
        
    def OnTextChanged(self, event):
        print 'OnTextChanged' 
        self.pWin.setFilter(self.tc.GetValue())
        
        #Popup()会阻碍tc获取焦点，导致Popup之后下一个输入无效
        #Show()会导致pWin变得好像不是子窗口，不会因为失去焦点而Dissmiss
        #因而需要增加事件处理EVT_KILL_FOCUS
        if not(self.pWin.IsShown()):
            self.pWin.Show()
            #self.pWin.Popup()
        self.tc.SetFocus()
            
    def SetValue(self, value):
        self.tc.SetValue(value)
        self.oldValue = value
    
    def GetValue(self):
        return self.tc.GetValue()
            
    def ChangeValue(self, value):
        print 'ChangeValue'
        self.tc.ChangeValue(value)
        self.oldValue = value
        
class MyPopupTextCtrlDecorator:
    '''封装客户搜索框'''
    def __init__(self, tc, pWin):
        '''
            tc - textctrl object
            pWin - 弹出窗口. 须实现setFilter(filterStr)
        '''
        self.tc = tc
        
        self.pWin = pWin
        self.resetPopupWinPos()
        
        self.oldValue = ''
        
        self.tc.Bind(wx.EVT_TEXT, self.OnTextChanged)
        #self.tc.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
    
    def resetPopupWinPos(self):
        posBtn = self.tc.ClientToScreen((0,0))
        posPrt = self.tc.GetParent().ClientToScreen((0,0))
        sz =  self.tc.GetSize()
        self.pWin.Move(wx.Point(posPrt.x, posBtn.y + sz.GetHeight()))
            
    def OnTextChanged(self, event):
        #print 'OnTextChanged' 
        # 如果是把输入全部删掉，则不再显示下拉框
        if(self.tc.GetValue() == ''):
            self.pWin.Show(False)
            return
        
        self.pWin.setFilter(self.tc.GetValue())
        
        # 141016 主窗口移动后，弹出窗口也要移动位置，所以每次都要resetPopupWinPos
        #if not(self.pWin.IsShown()):
        #    self.resetPopupWinPos()
        #    self.pWin.Show()
        self.resetPopupWinPos()
        self.pWin.Show()
        
        self.tc.SetFocus()
            
    def SetValue(self, value):
        self.tc.SetValue(value)
        self.oldValue = value
    
    def GetValue(self):
        return self.tc.GetValue()
            
    def ChangeValue(self, value):
        self.tc.ChangeValue(value)
        self.tc.SetInsertionPointEnd()
        self.oldValue = value


def only_driver(driverObj):
    '''只显示在职司机'''
    #print 'only_driver'
    #print driverObj.getValue(driverObj.COL_DISABLE)
    #print driverObj.getValue(driverObj.COL_JOB)
    return (driverObj.getValue(driverObj.COL_DISABLE) == 0) and (driverObj.getValue(driverObj.COL_JOB) & 1 == 1)

def only_enable_staff(staffObj):
    return staffObj.getValue(staffObj.COL_DISABLE) == 0

def only_enable_account(accountObj):
    return accountObj.getValue(accountObj.COL_DISABLED) == 0

if __name__ == '__main__':
    pass